'use strict';

const moment = require('moment');

const Controller = require('egg').Controller;

class TaskController extends Controller {
  async getTaskList() {
    const { ctx } = this;
    const { state = 0, page = 1, page_size = 5 } = ctx.query;
    try {
      const list = await ctx.service.task.list(state);
      ctx.body = {
        code: 200,
        msg: '请求成功',
        data: list,
      };
    } catch (error) {
      console.log('err', error);
      ctx.body = {
        code: 500,
        msg: '系统错误',
        data: null,
      };
    }
  }
  async add() {
    const { ctx } = this;
    const { id, task, time, complete = moment(new Date(new Date(time).getTime() + (24 * 60 * 60 * 1000)), 'YYYY-MM-DD HH:mm:ss').format('YYYY-MM-DD HH:mm:ss'), state = 1 } = ctx.request.body;
    if (!task || !time) {
      ctx.body = {
        code: 400,
        msg: '参数错误',
        data: null,
      };
      return;
    }
    // 如果有id的话是修改，不是新增
    if (id) {
      try {
        await ctx.service.task.update({ id, task, time, complete, state });
        ctx.body = {
          code: 200,
          msg: '请求成功',
          data: null,
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: '系统错误',
          data: null,
        };
      }
      return;
    }
    try {
      const result = await ctx.service.task.add({
        task, time, complete, state,
      });
      if (result) {
        ctx.body = {
          code: 200,
          msg: '请求成功',
          data: null,
        };
      } else {
        ctx.body = {
          code: 211,
          msg: '添加失败',
          data: null,
        };
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '系统错误',
        data: null,
      };
    }
  }
  async delete() {
    const { ctx } = this;
    const { id } = ctx.query;
    if (!id) {
      ctx.body = {
        code: 400,
        msg: '参数错误',
        data: null,
      };
      return;
    }
    try {
      await ctx.service.task.delete(id);
      ctx.body = {
        code: 200,
        msg: '请求成功',
        data: null,
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '系统错误',
        data: null,
      };
    }
  }
  async update() {
    const { ctx } = this;
    const { id, state } = ctx.request.body;
    if (!id || !state) {
      ctx.body = {
        code: 400,
        msg: '参数错误',
        data: null,
      };
      return;
    }
    try {
      await ctx.service.task.update({ id, state });
      ctx.body = {
        code: 200,
        msg: '请求成功',
        data: null,
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '系统错误',
        data: null,
      };
    }
  }
  async edit() {
    const { ctx } = this;
    const { id } = ctx.query;
    if (!id) {
      ctx.body = {
        code: 400,
        msg: '参数错误',
        data: null,
      };
      return;
    }
    try {
      const result = await ctx.service.task.edit(id);
      ctx.body = {
        code: 200,
        msg: '请求成功',
        data: result && result[0],
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '系统错误',
        data: null,
      };
    }
  }
}
module.exports = TaskController;
